package com.project.controller.Cgw;

import cgw.YSinterface;
import com.Entity.DzmHisMemberEntity;
import com.Entity.DzmHisSchedulingEntity;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.Id;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@CrossOrigin(value = {"*"}, allowCredentials = "true")
public class YsControkller {

    @Reference
    private YSinterface ySinterface;

    @Resource
    private RedisTemplate<String, DzmHisMemberEntity> redisTemplate;

    @RequestMapping("spec")
    @ResponseBody
    public List<Map<String,Object>> Dzm(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String tok = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("token")) {
                tok = cookie.getValue();
                break;
            }
        }

        DzmHisMemberEntity dzm = redisTemplate.opsForValue().get(tok);
        Object typ = dzm.getType();
        Object ui = dzm.getUid();
       int type=Integer.parseInt(String.valueOf(typ));
       int uid=Integer.parseInt(String.valueOf(ui));
       if (dzm != null) {
            if (type == 1) {
                List<Map<String,Object>> dzms = ySinterface.querys(dzm);
                return dzms;
            } else {
                List<Map<String,Object>> dzmp = ySinterface.querys(dzm);
                return dzmp;
            }
        }
        return null;
    }




    @RequestMapping("caozuo")
    @ResponseBody
    public String lis(@RequestParam("registeredfee_id") List<Integer> registeredfee_id,String start_time_this_week,int physicianid,int departmentId) throws ParseException {
        System.out.println(departmentId);
        int ui=departmentId;
        Map<String,Object>mapsp=new HashMap<>();

        String start_time_thisweek=null;
        String sp1=null;

        if (start_time_this_week.length()<=13){
            start_time_thisweek=start_time_this_week.substring(0,9);
        }else if (start_time_this_week.length()<=13){
            start_time_thisweek=start_time_this_week.substring(0,8);
        }else {
            start_time_thisweek=start_time_this_week.substring(0,10);
        }

        /* ------------------------------*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date  currdate = format.parse(start_time_thisweek);
        String currdates=format.format(currdate);//开始
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, 6);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);//结束
        System.out.println(enddate);
        /* ------------------------------*/

        mapsp.put("physicianid",physicianid);
        mapsp.put("department_id",departmentId);
        mapsp.put("start_time_this_week",currdates);
        mapsp.put("end_time_this_week",enddate);
        List<Map<String,Object>>map=ySinterface.Selecctductor(mapsp);
        Long l = System.currentTimeMillis();//创建时间
        Long time = new Date().getTime();
        Long millis = Calendar.getInstance().getTimeInMillis();
        List<Object>date=new ArrayList<>();//存时间
        List<Object>registeredfee_ids=new ArrayList<>();//存registeredfee_id
        List<Object>scheduling_subsection_id=new ArrayList<>();//存scheduling_subsection_id
        if (map.size()>0){
            //修改时间
            int update_time=millis.intValue();
            DzmHisSchedulingEntity dzm=new DzmHisSchedulingEntity();
            dzm.setPhysicianid(physicianid);
            dzm.setDepartmentId(departmentId);
            dzm.setStartTimeThisWeek(currdates);
            dzm.setCompanyId(2);
            dzm.setEndTimeThisWeek(enddate);
            dzm.setCreateTime(update_time);
            dzm.setUpdateTime(update_time);
                Map<String,Object>msp=new HashMap<String, Object>();
                msp.put("start_time_this_week",currdates);
                msp.put("end_time_this_week",enddate);
                msp.put("department_id",departmentId);
                msp.put("physicianid",physicianid);
                int scheduling_id=ySinterface.scheduling_id(msp);
                        List<Integer>list=ySinterface.lis(scheduling_id);
                        String start=start_time_thisweek;
                        String times = null;
                        Date sDate = null;
                        date.add(currdates);
                        for (int i1=1;i1<=6;i1++){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            if (i1>1){
                                sDate = sdf.parse(times);
                            }else{
                                sDate= sdf.parse(start);
                            }
                            Format f = new SimpleDateFormat("yyyy-MM-dd");
                            Calendar c = Calendar.getInstance();
                            c.setTime(sDate);
                            c.add(Calendar.DAY_OF_MONTH, 1); //利用Calendar 实现 Date日期+1天
                            sDate = c.getTime();
                            times=String.valueOf(f.format(sDate));
                            date.add(times);//放入时间
                        }
                        for (int p=0;p<list.size();p++){
                            scheduling_subsection_id.add(list.get(0+p));//放入挂号id
                        }
                        for (int g=0;g<registeredfee_id.size();g++){
                            registeredfee_ids.add(registeredfee_id.get(0+g));//放入registeredfee_id
                        }



                    Map<String,Object>mb=new HashMap<>();
                    mb.put("date",date.get(0));
                    mb.put("registeredfee_id",registeredfee_id.get(0));
                    mb.put("scheduling_subsection_id",scheduling_subsection_id.get(0));
                    Map<String,Object>mc=new HashMap<>();
                    mc.put("date",date.get(1));
                    mc.put("registeredfee_id",registeredfee_id.get(1));
                    mc.put("scheduling_subsection_id",scheduling_subsection_id.get(0));
                    Map<String,Object>md=new HashMap<>();
                    md.put("date",date.get(2));
                    md.put("registeredfee_id",registeredfee_id.get(2));
                    md.put("scheduling_subsection_id",scheduling_subsection_id.get(0));
                    Map<String,Object>me=new HashMap<>();
                    me.put("date",date.get(3));
                    me.put("registeredfee_id",registeredfee_id.get(3));
                    me.put("scheduling_subsection_id",scheduling_subsection_id.get(0));
                    Map<String,Object>mf=new HashMap<>();
                    mf.put("date",date.get(4));
                    mf.put("registeredfee_id",registeredfee_id.get(4));
                    mf.put("scheduling_subsection_id",scheduling_subsection_id.get(0));
                    Map<String,Object>mg=new HashMap<>();
                    mg.put("date",date.get(5));
                    mg.put("registeredfee_id",registeredfee_id.get(5));
                    mg.put("scheduling_subsection_id",scheduling_subsection_id.get(0));
                    Map<String,Object>mh=new HashMap<>();
                    mh.put("date",date.get(6));
                    mh.put("registeredfee_id",registeredfee_id.get(6));
                    mh.put("scheduling_subsection_id",scheduling_subsection_id.get(0));
                    Map<String,Object>mi=new HashMap<>();
                    mi.put("date",date.get(0));
                    mi.put("registeredfee_id",registeredfee_id.get(7));
                    mi.put("scheduling_subsection_id",scheduling_subsection_id.get(1));
                    Map<String,Object>mj=new HashMap<>();
                    mj.put("date",date.get(1));
                    mj.put("registeredfee_id",registeredfee_id.get(8));
                    mj.put("scheduling_subsection_id",scheduling_subsection_id.get(1));
                    Map<String,Object>mk=new HashMap<>();
                    mk.put("date",date.get(2));
                    mk.put("registeredfee_id",registeredfee_id.get(9));
                    mk.put("scheduling_subsection_id",scheduling_subsection_id.get(1));
                    Map<String,Object>ml=new HashMap<>();
                    ml.put("date",date.get(3));
                    ml.put("registeredfee_id",registeredfee_id.get(10));
                    ml.put("scheduling_subsection_id",scheduling_subsection_id.get(1));
                    Map<String,Object>mm=new HashMap<>();
                    mm.put("date",date.get(4));
                    mm.put("registeredfee_id",registeredfee_id.get(11));
                    mm.put("scheduling_subsection_id",scheduling_subsection_id.get(1));
                    Map<String,Object>mn=new HashMap<>();
                    mn.put("date",date.get(5));
                    mn.put("registeredfee_id",registeredfee_id.get(12));
                    mn.put("scheduling_subsection_id",scheduling_subsection_id.get(1));
                    Map<String,Object>mo=new HashMap<>();
                    mo.put("date",date.get(6));
                    mo.put("registeredfee_id",registeredfee_id.get(13));
                    mo.put("scheduling_subsection_id",scheduling_subsection_id.get(1));
                    Map<String,Object>mp=new HashMap<>();
                    mp.put("date",date.get(0));
                    mp.put("registeredfee_id",registeredfee_id.get(14));
                    mp.put("scheduling_subsection_id",scheduling_subsection_id.get(2));
                    Map<String,Object>mq=new HashMap<>();
                    mq.put("date",date.get(1));
                    mq.put("registeredfee_id",registeredfee_id.get(15));
                    mq.put("scheduling_subsection_id",scheduling_subsection_id.get(2));
                    Map<String,Object>mr=new HashMap<>();
                    mr.put("date",date.get(2));
                    mr.put("registeredfee_id",registeredfee_id.get(16));
                    mr.put("scheduling_subsection_id",scheduling_subsection_id.get(2));
                    Map<String,Object>ms=new HashMap<>();
                    ms.put("date",date.get(3));
                    ms.put("registeredfee_id",registeredfee_id.get(17));
                    ms.put("scheduling_subsection_id",scheduling_subsection_id.get(2));
                    Map<String,Object>mt=new HashMap<>();
                    mt.put("date",date.get(4));
                    mt.put("registeredfee_id",registeredfee_id.get(18));
                    mt.put("scheduling_subsection_id",scheduling_subsection_id.get(2));
                    Map<String,Object>mv=new HashMap<>();
                    mv.put("date",date.get(5));
                    mv.put("registeredfee_id",registeredfee_id.get(19));
                    mv.put("scheduling_subsection_id",scheduling_subsection_id.get(2));
                    Map<String,Object>mw=new HashMap<>();
                    mw.put("date",date.get(6));
                    mw.put("registeredfee_id",registeredfee_id.get(20));
                    mw.put("scheduling_subsection_id",scheduling_subsection_id.get(2));


                    List<Map<String,Object>>lis1=new ArrayList<>();
                    lis1.add(mb);
                    List<Map<String,Object>>lis2=new ArrayList<>();
                    lis2.add(mc);
                    List<Map<String,Object>>lis3=new ArrayList<>();
                    lis3.add(md);
                    List<Map<String,Object>>lis4=new ArrayList<>();
                    lis4.add(me);
                    List<Map<String,Object>>lis5=new ArrayList<>();
                    lis5.add(mf);
                    List<Map<String,Object>>lis6=new ArrayList<>();
                    lis6.add(mg);
                    List<Map<String,Object>>lis7=new ArrayList<>();
                    lis7.add(mh);
                    List<Map<String,Object>>lis8=new ArrayList<>();
                    lis8.add(mi);
                    List<Map<String,Object>>lis9=new ArrayList<>();
                    lis9.add(mj);
                    List<Map<String,Object>>lis10=new ArrayList<>();
                    lis10.add(mk);
                    List<Map<String,Object>>lis11=new ArrayList<>();
                    lis11.add(ml);
                    List<Map<String,Object>>lis12=new ArrayList<>();
                    lis12.add(mm);
                    List<Map<String,Object>>lis13=new ArrayList<>();
                    lis13.add(mn);
                    List<Map<String,Object>>lis14=new ArrayList<>();
                    lis14.add(mo);
                    List<Map<String,Object>>lis15=new ArrayList<>();
                    lis15.add(mp);
                    List<Map<String,Object>>lis16=new ArrayList<>();
                    lis16.add(mq);
                    List<Map<String,Object>>lis17=new ArrayList<>();
                    lis17.add(mr);
                    List<Map<String,Object>>lis18=new ArrayList<>();
                    lis18.add(ms);
                    List<Map<String,Object>>lis19=new ArrayList<>();
                    lis19.add(mt);
                    List<Map<String,Object>>lis20=new ArrayList<>();
                    lis20.add(mv);
                    List<Map<String,Object>>lis21=new ArrayList<>();
                    lis21.add(mw);


                    ySinterface.psssd(mb);
                    ySinterface.psssd1(mc);
                    ySinterface.psssd2(md);
                    ySinterface.psssd3(me);
                    ySinterface.psssd4(mf);
                    ySinterface.psssd5(mg);
                    ySinterface.psssd6(mh);
                    ySinterface.psssd7(mi);
                    ySinterface.psssd8(mj);
                    ySinterface.psssd9(mk);
                    ySinterface.psssd10(ml);
                    ySinterface.psssd11(mm);
                    ySinterface.psssd12(mn);
                    ySinterface.psssd13(mo);
                    ySinterface.psssd14(mp);
                    ySinterface.psssd15(mq);
                    ySinterface.psssd16(mr);
                    ySinterface.psssd17(ms);
                    ySinterface.psssd18(mt);
                    ySinterface.psssd19(mv);
                    ySinterface.psssd20(mw);
            sp1="2";
        }else{
            //修改时间
            int update_time=millis.intValue();
            Map<String,Object>msqp=new HashMap<>();
            msqp.put("physicianid",physicianid);
            msqp.put("department_id",ui);
            msqp.put("start_time_this_week",currdates);
            msqp.put("end_time_this_week",enddate);
            msqp.put("create_time",update_time);
            msqp.put("update_time",update_time);
            System.out.println(ui);
            int i=ySinterface.spec(msqp);//-----------------------------

            Date spDate = null;
           if (i!=0){//插入我的排班不为空
               Map<String,Object>msp=new HashMap<String, Object>();
               msp.put("start_time_this_week",currdates);
               msp.put("end_time_this_week",enddate);
               msp.put("department_id",departmentId);
               msp.put("physicianid",physicianid);
               int scheduling_id=ySinterface.scheduling_id(msp);
               if (scheduling_id!=0){//查询排班id
                   int t1=ySinterface.insertCollectList(scheduling_id);
                   if (t1!=0){//插入 排班时段表成功不为空
                       List<Integer>list=ySinterface.lis(scheduling_id);
                       Map<String,Object>spea=new HashMap<>();
                       String start=currdates;
                       String times = null;
                       Date sDate = null;
                       spea.put("date0",currdates);
                       for (int i1=1;i1<=6;i1++){
                           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                           if (i1>1){
                               sDate = sdf.parse(times);
                           }else{
                                sDate= sdf.parse(start);
                           }
                           Format f = new SimpleDateFormat("yyyy-MM-dd");
                           Calendar c = Calendar.getInstance();
                           c.setTime(sDate);
                           c.add(Calendar.DAY_OF_MONTH, 1); //利用Calendar 实现 Date日期+1天
                           sDate = c.getTime();
                           times=String.valueOf(f.format(sDate));
                           spea.put("date"+i1,times);
                           System.out.println(spea.toString());
                       }
                       for (int p=0;p<list.size();p++){
                           spea.put("scheduling_subsection_id"+p,list.get(0+p));//放入挂号id
                       }
                          for (int g=0;g<21;g++){
                              spea.put("registeredfee_id"+g,registeredfee_id.get(g));//放入registeredfee_id
                          }

                          int sq=ySinterface.sp(spea);
                   }
               }
           }
           sp1="1";
       }
        return sp1;
    }


    @RequestMapping("xiala")
    @ResponseBody
    public List<Map<String,Object>> sele(){
       List<Map<String,Object>>map=ySinterface.selec();
        return map;
    }


    //上个星期
    @RequestMapping("sp")
    @ResponseBody
    public List<Map<String,Object>> sp(String id,String department_id,String start_time_this_week) throws ParseException {
        /* ------------------------------*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date  currdate = format.parse(start_time_this_week);
        String currdates=format.format(currdate);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, 5);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        /* ------------------------------*/
        Map<String,Object>mapsp=new HashMap<>();
        mapsp.put("physicianid",id);
        mapsp.put("department_id",department_id);
        mapsp.put("start_time_this_week",currdates);
        mapsp.put("end_time_this_week",enddate);
        List<Map<String,Object>>map=ySinterface.Selecctductor(mapsp);
        return map;
    }

    //下个星期
    @RequestMapping("so")
    @ResponseBody
    public List<Map<String,Object>> so(String id,String department_id,String start_time_this_week) throws ParseException {
        /* ------------------------------*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date  currdate = format.parse(start_time_this_week);
        String currdates=format.format(currdate);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, 5);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        /* ------------------------------*/
        Map<String,Object>mapsp=new HashMap<>();
        mapsp.put("physicianid",id);
        mapsp.put("department_id",department_id);
        mapsp.put("start_time_this_week",currdates);
        mapsp.put("end_time_this_week",enddate);
        List<Map<String,Object>>map=ySinterface.Selecctductor(mapsp);
        return map;
    }

    //查询到的医生排班
    @RequestMapping("findweek")
    @ResponseBody
    public List<Map<String,Object>> selectd(String id,String department_id,String start_time_this_week,String end_time_this_week) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date  currdate = format.parse(start_time_this_week);
        String currdates=format.format(currdate);//开始
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, 5);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);//结束
        /* ------------------------------*/
        Map<String,Object>mapsp=new HashMap<>();
        mapsp.put("physicianid",id);
        mapsp.put("department_id",department_id);
        mapsp.put("start_time_this_week",currdates);
        mapsp.put("end_time_this_week",enddate);
        List<Map<String,Object>>map=ySinterface.Selecctductor(mapsp);

        return map;
    }


    //显示患者
    @RequestMapping("sdfg")
    @ResponseBody
    public ResponseEntity<?> mso(){
        List<Map<String,Object>>aqs=ySinterface.lp();
        return new ResponseEntity<List>(aqs, HttpStatus.OK);
    }

    //查询患者
    @RequestMapping("mpsq")
    @ResponseBody
    public ResponseEntity<?> msos(String pid){
        List<Map<String,Object>>aqs=ySinterface.hz(pid);
        return new ResponseEntity<List>(aqs, HttpStatus.OK);
    }
}
